{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Slots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's start with an example of how we use slots:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Location:\n",
    "    __slots__ = 'name', '_longitude', '_latitude'\n",
    "    \n",
    "    def __init__(self, name, longitude, latitude):\n",
    "        self._longitude = longitude\n",
    "        self._latitude = latitude\n",
    "        self.name = name\n",
    "        \n",
    "    @property\n",
    "    def longitude(self):\n",
    "        return self._longitude\n",
    "    \n",
    "    @property\n",
    "    def latitude(self):\n",
    "        return self._latitude"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Location` still has that mapping proxy, and we can still add and remove **class** attributes from `Location`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mappingproxy({'__module__': '__main__',\n",
       "              '__slots__': ('name', '_longitude', '_latitude'),\n",
       "              '__init__': <function __main__.Location.__init__(self, name, longitude, latitude)>,\n",
       "              'longitude': <property at 0x7feed0329ae8>,\n",
       "              'latitude': <property at 0x7feed0329b38>,\n",
       "              '_latitude': <member '_latitude' of 'Location' objects>,\n",
       "              '_longitude': <member '_longitude' of 'Location' objects>,\n",
       "              'name': <member 'name' of 'Location' objects>,\n",
       "              '__doc__': None})"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Location.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Location.map_service = 'Google Maps'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mappingproxy({'__module__': '__main__',\n",
       "              '__slots__': ('name', '_longitude', '_latitude'),\n",
       "              '__init__': <function __main__.Location.__init__(self, name, longitude, latitude)>,\n",
       "              'longitude': <property at 0x7feed0329ae8>,\n",
       "              'latitude': <property at 0x7feed0329b38>,\n",
       "              '_latitude': <member '_latitude' of 'Location' objects>,\n",
       "              '_longitude': <member '_longitude' of 'Location' objects>,\n",
       "              'name': <member 'name' of 'Location' objects>,\n",
       "              '__doc__': None,\n",
       "              'map_service': 'Google Maps'})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Location.__dict__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But the use of `slots` affects **instances** of the class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = Location('Mumbai', 19.0760, 72.8777)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Mumbai', 19.076, 72.8777)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.name, l.longitude, l.latitude"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The **instance** no longer has a dictionary for maintaining state:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'Location' object has no attribute '__dict__'\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    l.__dict__\n",
    "except AttributeError as ex:\n",
    "    print(ex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This means we can no longer add attributes to the instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'Location' object has no attribute 'map_link'\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    l.map_link = 'http://maps.google.com/...'\n",
    "except AttributeError as ex:\n",
    "    print(ex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can actually delete the attribute from the instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "del l.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And as we can see the instance now longer has that attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Attribute Error: name\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print(l.name)\n",
    "except AttributeError as ex:\n",
    "    print(f'Attribute Error: {ex}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However we can still re-assign a value to that same attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "l.name = 'Mumbai'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Mumbai'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mainly we use slots when we expect to have many instances of a class and to gain a performance boost (mostly storage, but also attribute lookup speed). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
